package com.rt.easyjava.base.modules.timerjob.service.impl.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rt.easyjava.base.modules.timerjob.bean.TmptTimerLogBiz;
import com.rt.easyjava.base.modules.timerjob.dto.TmptTimerLogBizDto;
import com.rt.easyjava.base.modules.timerjob.mapper.TmptTimerLogBizMapper;
import com.rt.easyjava.base.modules.timerjob.service.intf.TmptTimerLogBizService;
import com.rt.easyjava.common.base.Result;
import com.rt.easyjava.common.config.dozer.MyMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.List;

/**
 *  服务实现类
 * @author wuxiaoyong
 * @since 2020-06-01
 */
public class TmptTimerLogServiceImpl extends ServiceImpl<TmptTimerLogBizMapper, TmptTimerLogBiz> implements TmptTimerLogBizService {
    private final Logger logger = LoggerFactory.getLogger(TmptTimerLogServiceImpl.class);

    @Autowired
    protected MyMapper mapper;
    @Resource
    protected TmptTimerLogBizMapper tmptTimerLogBizMapper;

    // ==================================================================================
    // ========== 以下使用的是 Mybatis-plus 实现, 适合单表操作 ==============================
    // ==================================================================================

    /**
    * create a object
    * @param tmptTimerLogBizDto
    * @return
    */
    @Override
    public Result<TmptTimerLogBizDto> create(TmptTimerLogBizDto tmptTimerLogBizDto) {
        Result<TmptTimerLogBizDto> result = new Result<>();

        // dto to entity
        TmptTimerLogBiz tmptTimerLogBiz = mapper.map(tmptTimerLogBizDto, TmptTimerLogBiz.class);

        tmptTimerLogBizMapper.insert(tmptTimerLogBiz);

        return result.success();
    }

    /**
    * update a object by Id
    * @param tmptTimerLogBizDto
    * @param updateWrapper
    * @return
    */
    @Override
    public Result<TmptTimerLogBizDto> update(TmptTimerLogBizDto tmptTimerLogBizDto, UpdateWrapper updateWrapper) {
        Result<TmptTimerLogBizDto> result = new Result<>();

        // dto to entity
        TmptTimerLogBiz tmptTimerLogBiz = mapper.map(tmptTimerLogBizDto, TmptTimerLogBiz.class);

        tmptTimerLogBizMapper.update(tmptTimerLogBiz, updateWrapper);

        return result.success();
    }

    /**
    * 分页 (无合计功能)
    * @param page
    * @param queryWrapper
    * @return
    */
    public Result<TmptTimerLogBizDto> page(Page<TmptTimerLogBiz> page, QueryWrapper<TmptTimerLogBiz> queryWrapper){
        Result<TmptTimerLogBizDto> result = new Result<>();

        // mapper
        IPage<TmptTimerLogBiz> iPage = tmptTimerLogBizMapper.selectPage(page, queryWrapper);

        // 查询结果转换成DTO
        List<TmptTimerLogBizDto> tmptTimerLogBizDtoList= mapper.maplist(iPage.getRecords(), TmptTimerLogBizDto.class);

        result.getMap().put("total", iPage.getTotal());
        result.getMap().put("rows", tmptTimerLogBizDtoList);

        return result.success();
    }

    /**
    * select all by queryWrapper
    * @param queryWrapper
    * @return
    */
    @Override
    public Result<TmptTimerLogBizDto> selectBy(QueryWrapper<TmptTimerLogBiz> queryWrapper) {
        Result<TmptTimerLogBizDto> result = new Result<>();

        List<TmptTimerLogBiz> list = tmptTimerLogBizMapper.selectList(queryWrapper);

        List<TmptTimerLogBizDto> dtoList = mapper.maplist(list, TmptTimerLogBizDto.class);

        return result.setList(dtoList).success();
    }

    /**
    * select one by id
    * @param id
    * @return
    */
    @Override
    public Result<TmptTimerLogBizDto> selectById(Long id) {
        Result<TmptTimerLogBizDto> result = new Result<>();

        TmptTimerLogBiz tmptTimerLogBiz = tmptTimerLogBizMapper.selectById(id);

        TmptTimerLogBizDto tmptTimerLogBizDto = mapper.map(tmptTimerLogBiz, TmptTimerLogBizDto.class);

        return result.setObj(tmptTimerLogBizDto).success();
    }




    // ==================================================================================
    // ========== 以下使用的是自定义的 mapper.xml 实现, 适合多表查询 =========================
    // ==================================================================================

    /**
    * 分页查询：mybatis plus 自定义sql分页查询
    * 默认开启合计查询，按需注掉代码
    * @param page
    * @param queryWrapper
    * @return
    */
    @Override
    public Result<TmptTimerLogBizDto> myPage(Page<TmptTimerLogBiz> page, QueryWrapper<TmptTimerLogBiz> queryWrapper) {
        Result<TmptTimerLogBizDto> result = new Result();

        IPage<TmptTimerLogBiz> iPage = tmptTimerLogBizMapper.myPage(page, queryWrapper);
        // 查询结果转换成DTO
        List<TmptTimerLogBizDto> tmptTimerLogBizDtoList = mapper.maplist(iPage.getRecords(), TmptTimerLogBizDto.class);
        // 返回结果集
        result.getMap().put("total", iPage.getTotal());
        result.getMap().put("rows", tmptTimerLogBizDtoList);

        // 合计
        List<TmptTimerLogBiz> sumList = tmptTimerLogBizMapper.mySelectSum(queryWrapper);
        List<TmptTimerLogBizDto> sumDtoList = mapper.maplist(sumList, TmptTimerLogBizDto.class);
        result.getMap().put("footer", sumDtoList);

        return result.success();
    }

    @Override
    public Result<TmptTimerLogBizDto> mySelectBy(QueryWrapper<TmptTimerLogBiz> queryWrapper) {
        Result<TmptTimerLogBizDto> result = new Result();

        List<TmptTimerLogBiz> list = tmptTimerLogBizMapper.mySelectBy(queryWrapper);
        List<TmptTimerLogBizDto> dtoList = mapper.maplist(list, TmptTimerLogBizDto.class);
        result.setList(dtoList);

        return result.success();
    }

    @Override
    public Result<TmptTimerLogBizDto> mySelectById(long id) {
        Result<TmptTimerLogBizDto> result = new Result();

        TmptTimerLogBiz tmptTimerLogBiz = tmptTimerLogBizMapper.mySelectById(id);
        TmptTimerLogBizDto dto = mapper.map(tmptTimerLogBiz,TmptTimerLogBizDto.class);
        result.setObj(dto);

        return result.success();
    }


}
